package edu.gatech.ealf.magiclenseplaf;


import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;

public class MLButtonUI extends ButtonUI implements MouseListener {

    
    
    private ZoomHelper zoomer = null;
    
    public MLButtonUI() {
        super();
    }

    public static ComponentUI createUI(JComponent c) {
        return new MLButtonUI();
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        MagicLenseLookAndFeel.getInstance().registerComponentInstallation(this, c);
        zoomer = new ZoomHelper(c);
        c.addMouseListener(this);
    }

    public void paint(Graphics g, JComponent c) {
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        MagicLenseLookAndFeel.getInstance().registerComponentUninstallation(this, c);
        c.removeMouseListener(this);
        if(zoomer != null){
            zoomer.destroy();
        }
        zoomer = null;
    }

    @Override
    public void update(Graphics g, JComponent c) {
        paint(g, c);
    }

    public void mouseClicked(MouseEvent e) {
        if(e.getButton()== MouseEvent.BUTTON2){
            zoomer.toggleZoomable();
        }
    }


    public void mouseEntered(MouseEvent e) {
        //System.out.println("MLButtonUI.mouseEntered");
        if(zoomer.curComponent.getClass().getSimpleName().equals("JButton")){
            zoomer.zoom();
        }
	}

    public void mouseExited(MouseEvent e) {
        if(zoomer.curComponent.getClass().getSimpleName().equals("JButton")){
            boolean hasFocus = zoomer.curComponent.hasFocus();
            zoomer.unZoom();
            if(hasFocus){
                zoomer.curComponent.requestFocusInWindow();
            }
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
}
